package com.jb.web.bean.list;

import com.jb.faces.component.UIComponent;
import com.jb.web.bean.Image;
import com.jb.web.bean.Label;
import com.jb.web.bean.Link;
import com.jb.web.bean.ListBean;
import com.jb.web.bean.TextField;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.FloatPanelRenderer;

import java.util.Iterator;


public class PagingRenderer extends FloatPanelRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        resolve((Paginator)itemToRender);
        return super.render(pCtx, itemToRender);
    }
    private ListBean getList(Paginator hItem) {
        return (ListBean)hItem.getRoot().getDescendantByClientId(hItem.getListId());
    }
    private boolean resolve(Paginator item) {
        ListBean lb = getList(item);
        if (lb == null)
            return false;
        Iterator list = item.getFacetsAndChildren();
        while (list.hasNext()) {
            UIComponentImpl child = (UIComponentImpl)list.next();
            if (child instanceof Link)
            {
                configLink(child, lb, item.getClientId());
            }   
            if (child instanceof Label) {
                String value = ((Label)child).getDefaultValue();
                if (value != null && value.equals("total")) {
                    ((Label)child).setValue("" + lb.getPages());
                }
            }

            if (child instanceof TextField) {
                ((TextField)child).setValue("${" + lb.getClientId() + ".currentPage+1}");
                ((TextField)child).setOnAutoSubmit("JBListBean.page(event, '"
                                                   +lb.getClientId()
                                                   +"',$('"+child.getClientId()
                                                   +"').value,{source:'"+item.getClientId()+"'});");
            }
        }
        return true;
    }
    private void configLink(UIComponent item, ListBean lb, String ctrlId) {
        Link hLink = (Link)item;
        String listId = lb.getClientId();
        String value = hLink.getDefaultValue();
        if ("next".equalsIgnoreCase(value) ) {
            hLink.setDisabled(lb==null||lb.isLast());
            if(hLink.isDisabled())
                hLink.setOnClick(null);
            else
                hLink.setOnClick("JBListBean.next('"+listId+"',{source:'"+ctrlId+"'});");
        }
        if ("last".equalsIgnoreCase(value)) {
            hLink.setDisabled(lb==null||lb.isLast());
            if(hLink.isDisabled())
                hLink.setOnClick(null);
            else
                hLink.setOnClick("JBListBean.last('"+listId+"',{source:'"+ctrlId+"'});");
        }
        if ("previous".equalsIgnoreCase(value)) {
            hLink.setDisabled(lb==null||lb.isFirst());
            if(hLink.isDisabled())
                hLink.setOnClick(null);
            else
                hLink.setOnClick("JBListBean.previous('"+listId+"',{source:'"+ctrlId+"'});");
        }
        if ("first".equalsIgnoreCase(value)) {
            hLink.setDisabled(lb==null||lb.isFirst());
            if(hLink.isDisabled())
                hLink.setOnClick(null);
            else
            hLink.setOnClick("JBListBean.first('"+listId+"',{source:'"+ctrlId+"'});");
        }
    }
    public StringBuffer renderChildren(WebContext pCtx, 
                                       UIComponent itemToRender) {
        Paginator sect = (Paginator)itemToRender;
        ListBean lb = getList(sect);

        StringBuffer html = new StringBuffer("<table ><tbody><tr><td>");
        Image comp =(Image) sect.getFacetOrChildByClientId(sect.getClientId()+"first");
        html.append(comp.render(pCtx)).append("</td><td>");
        
        comp =(Image) sect.getFacetOrChildByClientId(sect.getClientId()+"prev");
        html.append(comp.render(pCtx)).append("</td><td>");
        UIComponentImpl label =(UIComponentImpl) sect.getFacetOrChildByClientId(sect.getClientId()+"page");
        
        html.append(label.render(pCtx)).append("</td><td>");
        //goto page
        UIComponentImpl field =(UIComponentImpl) sect.getFacetOrChildByClientId(sect.getClientId()+"pageField");
        //comp.setOnKeyPress("")
        html.append(field.render(pCtx)).append("</td><td>");
        field =(UIComponentImpl) sect.getFacetOrChildByClientId(sect.getClientId()+"total");
        if(sect.getListId()!=null)
            field.setValue(" of total ${"+sect.getListId()+".pages} pages");
        html.append(field.render(pCtx)).append("</td><td>");
        comp =(Image) sect.getFacetOrChildByClientId(sect.getClientId()+"next");

        html.append(comp.render(pCtx)).append("</td><td>");
        comp =(Image) sect.getFacetOrChildByClientId(sect.getClientId()+"last");

        html.append(comp.render(pCtx)).append("</td><td></tr></tbody></table>");
               
        return html;
    }
}
